<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>新建链接</title>
    <link rel="stylesheet" href="./assets/css/bootstrap.css">

    <script src="assets/js/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container">
    <form action="./link/sss" method="post" enctype="multipart/form-data">
        <input type="hidden" name="id" value="0">
        <div class="form-group">
            <label>名称</label>
            <input type="text" name="name" class="form-control">
        </div>
        <div class="form-group">
            <label>图标</label>
            <input type="file" name="icon" class="form-control">
        </div>
        <div class="form-group">
            <label>描述</label>
            <input type="text" name="description" class="form-control">
        </div>
        <div class="form-group">
            <label>访问URL</label>
            <input type="url" name="url" class="form-control">
        </div>
        <div class="form-group">
            <label>缩略图</label>
            <input type="file" name="picture" class="form-control">
        </div>
        <div class="form-group">
            <label>详细内容</label>
            <div id="content_editor"></div>
            <textarea name="content" id="content_value" class="form-control"></textarea>
        </div>
        <div class="form-group">
            <label>分类编号</label>
            <input type="text" name="categoryId" class="form-control">
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="提交">
        </div>
    </form>
</div>
<script>
    $(function(){
        if(location.search){
            $.get('link/' + location.search.substr(1),function (link){
                console.log(link)
                $('[name=id]').val(link.id);
                $('[name=name]').val(link.name);
                $('[name=description]').val(link.description);
                $('[name=url]').val(link.url);
                $('#content_value').val(link.content);
                $('[name=categoryId]').val(link.categoryId);
            },'json');
        }
        $('form').on('submit',function(){
            var data = $(this).serializeArray();
            var params = {};
            for(var i = 0;i < data.length; i ++){
                var key = data[i].name,
                    value = data[i].value;
                params[key] = value;
            }
            console.log(params)
            $.ajax({
                method:'post',
                url:'link/update',
                data:JSON.stringify(params),
                contentType:'application/json',
                success:function (ret){
                    console.log(ret)
                }
            })
            return false;
        })
    });
</script>
</body>
</html>